package org.symkom.card;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.symkom.expression.Expression;
import org.symkom.expression.Operation;
import org.symkom.expression.SKObject;
import org.symkom.main.MainWindow;
import org.symkom.states.States;
public class ParametersPane extends Card{
	
	private JPanel paramsPane;
	private List<JTextField> params;
	
	private JPanel titlePane;
	private JPanel middlePane;
	private JPanel navigationPane;
	
	public ParametersPane(MainWindow mainWindow, Card prevCard) {
		super(mainWindow, prevCard);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void init() {
		super.init();
		
		setLayout(new BorderLayout());
		
		titlePane = new JPanel(new FlowLayout());
		middlePane = new JPanel(new FlowLayout());
		navigationPane = new JPanel(new FlowLayout());
		
		titlePane.add(new JLabel("Podaj wartosci wspolczynnikow: "));
		
		paramsPane = new JPanel(new GridLayout(function.size()/2 + 1, 2, 10, 10));
		params = new ArrayList<JTextField>();
		for(SKObject sk : function) {
			if(sk instanceof Expression) {
				JPanel pane = new JPanel();
				JLabel label = new JLabel(((Expression) sk).getOwnIndex());
				pane.add(label);
				
				JTextField text = new JTextField();
				text.setColumns(3);
				params.add(text);
				pane.add(text);
	
				paramsPane.add(pane);
			}
		}
		middlePane.add(paramsPane);
		
		navigationPane.add(nextButton);
		
		add(titlePane, BorderLayout.NORTH);
		add(middlePane, BorderLayout.CENTER);
		add(navigationPane, BorderLayout.SOUTH);
	}

	@Override
	protected void nextStage() {
		
		int k = 0;
		for(int i = 0 ; i < function.size() ; i++) {
			if(i%2 == 0)
				try{
					((Expression) function.get(i)).setValue(Double.valueOf(params.get(k++).getText()));
					mainWindow.setState(States.GRAPH, this);
				}catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Nie podano poprawnych parametrow!",
							"Blad", JOptionPane.ERROR_MESSAGE); 
					break;
				}
		}
	}



}
